Skip to content

fix: seeding cleanup removes a torrent shared by finished requests - #464

Merged
kikootwo merged 3 commits into
mainfrom
fix/283-shared-torrent-cleanup
Oct 27, 2026
Merged

kikootwo merged 3 commits into
mainfrom
fix/283-shared-torrent-cleanup

Conversation

@kikootwo

@kikootwo kikootwo commented Oct 27, 2026 •

Copy link
Copy Markdown
Owner

Closes #283

What changed

  • Seeding cleanup (src/lib/processors/cleanup-seeded-torrents.processor.ts): before removing a torrent, it now asks otherActiveRequestsSharingTorrent() for the other non-deleted requests on that torrent, with their type, status and download rows. Each of them keeps the torrent unless siblingKeepsTorrent() (src/lib/utils/shared-torrent.ts) finds it:
    • finished: audiobook available or ebook downloaded (FINISHED_REQUESTS, also the job's own candidate statuses);
    • for an audiobook, also imported by RMAB since it took the torrent: its book record completed with a filePath (only the organizer writes that, after the copy) and completedAt later than its row for this torrent. A library scan can mark a request available while the copy is still running, and a library match alone never passes;
    • recorded for the configured client type, on every row it has for the torrent;
    • done seeding by its own indexer's rule (evaluateSeeding() with that row's indexer settings, on the same client answer). An unlimited or unconfigured indexer keeps it.
  • When nothing keeps it: one deleteDownload, this request's row marked removed (a soft-deleted request is hard-deleted, as before), and the other requests' rows marked removed in one updateMany. A row RMAB did not add (a series pack the 2nd and 3rd request found already in the client) never reaches this point on its own, so this is the only way its row learns the torrent is gone.
  • The skip log now names each request that keeps the torrent and why (req-x (audiobook downloading), req-y (still seeding for Tracker: ratio --/--, time 40/600 min)).
  • Request deletion is unchanged: otherActiveRequestsUsingTorrent() is untouched and still keeps the torrent for any other active request.
  • Docs: documentation/admin-features/request-deletion.md (cleanup rule, Fixed Issues Fix broken matching feature on homepage #6), one known-limit line in documentation/phase3/download-clients.md, the cleanup steps in documentation/phase3/file-organization.md.

Why

Three requests shared one series-pack torrent, all available, seeding requirement met. The cleanup's shared-torrent check counted every non-deleted request on the torrent, so each finished request kept it for the other two, every run. The issue's log shows the same three requests skipping each other.

Evidence

  • New tests/processors/cleanup-seeded-torrents.shared.test.ts runs the real processor with a findMany that answers the page query and the shared-torrent query from fixture requests:
    • three finished requests, requirement met: one deleteDownload, all three rows removed, no errors;
    • the same with two rows that took the pack over (addedByRmab: false) and the owner processed last: one delete, all three rows removed;
    • torrent already gone: each row marked removed through the probe, no errors;
    • another request downloading, processing, audiobook downloaded or failed: nothing removed;
    • a finished request whose own indexer asks for longer seeding, whose indexer is not configured, or recorded for another client: nothing removed;
    • an available audiobook RMAB has not imported from the torrent (copy still running, only matched in the library, or the book's only import older than its row): nothing removed; once RMAB has imported it: removed;
    • a soft-deleted owner whose only active sibling is finished and done seeding: one delete, the sibling's row removed, the owner hard-deleted;
    • a soft-deleted request still doesn't keep it.
  • tests/services/request-delete.seeding.test.ts: new case, a finished, done-seeding request still keeps the torrent when another request is deleted.
  • With main's processor and shared-torrent.ts (ece1559) and the two test files: 4 failed, 30 passed (every case that expects the removal: deleteDownload called 0 times). On this branch: 34/34.

Checks

  • Unit tests for the changed code (vitest related on shared-torrent.ts and the processor, the new test file included): PASS, 84 files, 1089/1089
  • Type check and lint: PASS (tsc --noEmit clean; ESLint on the touched code and test files: no new findings compared with main)
  • Image build + E2E on the branch image before the import check was added (fd0d63b; 242-torrent-seeding, 242-delete-guard with media-bind, one stack each): PASS, 242-torrent-seeding 1/1, 242-delete-guard 12/12. The import check only changes the shared-torrent decision, which the unit tests above cover
  • Full unit suite: PR Tests (CI)

Risk and rollback

  • Newly removed: torrents shared by several requests where every other active request on it is finished, recorded for the configured client, and done seeding by its own indexer's rule, with files (deleteDownload(id, true), as for a torrent only one request uses). This includes a soft-deleted request whose active siblings all meet that rule: it now removes the torrent and is hard-deleted, where before one finished sibling kept it. A finished request has been imported or matched in the library, so removal is the same as for a torrent one request uses: a hard link or a separate copy in the library survives it, and a layout where the library path is the download path does not (unchanged from today).
  • Only RMAB's own torrent: the ownership checks (knownNotOwned(), resolveOwnership()) still run first on the row that removes it. A torrent RMAB did not add for that row is kept, exactly as before.
  • A request still in progress keeps it: every status other than audiobook available and ebook downloaded keeps the torrent, so a request downloading, processing, waiting for import or failed keeps its files. An audiobook sibling also needs RMAB's own import from this torrent to be complete, so one a library scan marked available mid-copy keeps it, and one only matched in the library keeps it as on main. A torrent only one request uses is decided as before.
  • Different seeding rules: a request from an indexer that asks for more seeding (or seeds forever) keeps the torrent until its own rule is met.
  • Request deletion decides exactly as before.
  • Rows marked removed stay removed; nothing else is rewritten.
  • Rollback: revert this PR. Torrents already removed stay removed; rows marked removed are correct either way.

Release note

🤖 Generated with Claude Code

kikootwo and others added 3 commits October 27, 2026 01:49
#283)

The cleanup job's shared-torrent check counted every non-deleted request on
the torrent, so several finished requests (a series pack) kept it for each
other forever. In the cleanup job, another request now keeps the torrent
unless it is finished (audiobook available, ebook downloaded), recorded for
the configured client, and done seeding by its own indexer's rule. When the
torrent goes, the other requests' rows are marked removed too. Request
deletion keeps today's rule: any other active request keeps the torrent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…port from it (#283)

A library scan can mark an audiobook available while its organize is still
copying. The seeding cleanup now lets such a sibling release the torrent only
when its book record was completed by the organizer (with a file path) after
the sibling's row took the torrent. Also tests a soft-deleted owner removing a
torrent whose only active sibling is finished and done seeding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kikootwo
kikootwo marked this pull request as ready for review October 27, 2026 06:46
@kikootwo
kikootwo merged commit dad0daf into main Oct 27, 2026
1 check passed
@kikootwo
kikootwo deleted the fix/283-shared-torrent-cleanup branch October 27, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A download which belongs to multiple requests is never cleaned up

1 participant